package com.ruoyi.factory.ykj.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.factory.qylock.utils.OkHttpUtils;
import com.ruoyi.factory.qylock.utils.SignHelper;
import com.ruoyi.factory.ykj.service.LoginByCodeService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName LoginByCodeService
 * @Description: TODO
 * @Author LiuJiaji
 * @Date 2023/4/23 0023 10:58
 **/
@Service
public class LoginByCodeServiceImpl implements LoginByCodeService {
    protected final Logger logger = LoggerFactory.getLogger(LoginByCodeServiceImpl.class);

//    @Autowired
//    private CaptchaController captchaController;
//    @Autowired
//    private ISysUserService userService;
//    @Autowired
//    private ISysRoleService roleService;
    /**
     * @author: Liujiaji
     * @date: 2023/4/23 0023 11:02
     * @description: TODO 根据code获取用户信息
     * @param: [request]
     * @return: com.ruoyi.common.core.domain.AjaxResult
     **/
    @Override
    public AjaxResult getInfoByCode(Map<String,String> request){
        //处理请求参数 校验参数
        String param = request.get("code");
        if(StringUtils.isEmpty(param)) return new AjaxResult(300,"登录信息验证失败,请联系管理员");
        String code = param;
        //根据code请求友空间获取员工信息
        //获取token
        String result = SignHelper.getToken();
        if(StringUtils.isEmpty(result)) return new AjaxResult(300,"token验证失败,请联系管理员");
        String url = "https://c2.yonyoucloud.com/iuap-api-gateway/yonbip/uspace/freelogin/nc_info_by_code";
        Map<String, String> params = new HashMap<>();
        params.put("access_token",result);
        params.put("code",code);
        String s = JSONObject.fromObject(params).toString();
        logger.info("请求友空间根据code获取用户信息入参: "+s);
        String s1 = OkHttpUtils.okHttpGet(url, params);
        logger.info("请求友空间根据code获取用户信息返参: "+s1);
        JSONObject jsonObject1 = JSONObject.fromObject(s1);
        if(!"200".equals(jsonObject1.optString("code"))) return new AjaxResult(300,"登录信息验证失败,请重试");
        String data = jsonObject1.optString("data");
        JSONObject jsonObject2 = JSONObject.fromObject(data);
        String yhtUserId = jsonObject2.optString("yhtUserId");
        //根据userId获取员工详细信息
        String url2 = "https://c2.yonyoucloud.com/iuap-api-gateway/yonbip/uspace/staff/info_by_user_id";
        Map<String, String> pmap = new HashMap<>();
        pmap.put("access_token",result);
        pmap.put("yhtUserId",yhtUserId);
        logger.info("使用友互通userId查询员工信息入参: "+pmap.toString());
        String s4 = OkHttpUtils.okHttpGet(url2, pmap);
        logger.info("使用友互通userId查询员工信息返参: "+s4);
        JSONObject jsonObject3 = JSONObject.fromObject(s4);
        String code1 = jsonObject3.optString("code");
        if(!"200".equals(code1)) return new AjaxResult(300,"获取登录信息失败,请联系管理员");
        String data1 = jsonObject3.optString("data");
        JSONArray jsonArray = JSONArray.fromObject(data1);
        if(jsonArray.isEmpty()) return new AjaxResult(300,"获取登录信息失败,请联系管理员");
        Object o = jsonArray.get(0);
        JSONObject jsonObject = JSONObject.fromObject(o);
        //封装返回信息并同步数据库
        String id = jsonObject.optString("id");
        String code3 = jsonObject.optString("code");
        String name = jsonObject.optString("name");
        String sex = jsonObject.optString("sex");
        String mainjobinfo = jsonObject.optString("mainjobinfo");
        JSONArray jsonArray1 = JSONArray.fromObject(mainjobinfo);
        if(jsonArray1.isEmpty()) return new AjaxResult(300,"获取登录信息失败,请联系管理员");
        Object o1 = jsonArray1.get(0);
        JSONObject jsonObject4 = JSONObject.fromObject(o1);
        String dept_id1 = jsonObject4.optString("dept_id");
        String post_id1 = jsonObject4.optString("post_id");

        SysUser sysUser = new SysUser();
        sysUser.setUserId(Long.valueOf(id));
        sysUser.setUserName(code3);//账号
        sysUser.setNickName(name);
        //男-0 女-1
        sysUser.setSex(sex.equals("1") ? "0" : "1");
        sysUser.setPassword(SecurityUtils.encryptPassword("123456"));
        sysUser.setStatus("0");
        sysUser.setCreateBy("admin");
        sysUser.setDeptId(Long.valueOf(dept_id1));
        if(StringUtils.isNotEmpty(post_id1)){
            Long[] postid = {Long.valueOf(post_id1)};
            sysUser.setPostIds(postid);//岗位
        }

        //请求登录并验证
//        AjaxResult code2 = null;
//        try {
//            code2 = captchaController.getCode(null);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//        String uuid = (String)code2.get("uuid");
//
//
//        LoginBody loginBody = new LoginBody();
//        loginBody.setUuid(uuid);
//        loginBody.setUsername(code3);
//        loginBody.setPassword("123456");
        AjaxResult ajaxResult = new AjaxResult(200,"success",sysUser);
        return ajaxResult;
    }


}

